package com.bike.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhCity;
import com.bike.mapper.HhCityMapper;
import com.bike.util.PageData;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhCityServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhCityMapper hhCityMapper;
	
	/**
	* 保存
	*/
	public int saveOrUpdate(PageData pd, HhCity record) {
		
		record.getParentid();
		if(record.getLevel()==1){
			record.setParentid(0L);
		}else{
			HhCity city=hhCityMapper.selectByPrimaryKey(record.getParentid());
			if(city==null||city.getLevel()==3){
			 return 0;
			}
			if(city.getLevel()==1){//省
				record.setLevel(2);
				record.setParentName(city.getTitle());
			}else if(city.getLevel()==2){//市
				record.setLevel(3);
				record.setParentName(city.getTitle());
				record.setRootid(city.getParentid());
				record.setRootName(city.getParentName());
			}
		}
	 
		HhCity city=hhCityMapper.selectByPrimaryKey(record.getItemid());
		if(city!=null){
			 //编辑
			return hhCityMapper.updateByPrimaryKeySelective(record);
		}else{
			return hhCityMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhCityById(long itemid) {
		return hhCityMapper.deleteByPrimaryKey(itemid);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhCityList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<HhCity> records= hhCityMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhCity>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhCity> listAll(PageData pd) {
		return  hhCityMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhCity selectByHhCityId(Long itemid) {
		return hhCityMapper.selectByPrimaryKey(itemid);
	}

	public List<HhCity> selectPkCityListByParentId(PageData pd) {
		return hhCityMapper.selectByParentId(pd);
	}
}

